package com.woorigil.social.worker;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.woorigil.social.domain.history.WorkHistory;
import com.woorigil.social.domain.history.WorkHistoryDao;
import com.woorigil.social.domain.subscribe.Mail;
import com.woorigil.social.domain.subscribe.MailDao;
import com.woorigil.social.domain.subscribe.ReadMail;
import com.woorigil.social.parser.MailParserFactory;

//@Component
public class MailLoadWorker {
	public static final String WORK_NAME = "MAIL_LOAD";
	@Autowired MailParserFactory mailParserFactory;
	@Autowired WorkHistoryDao workHistoryDao;
	@Autowired ReadMail readMail;
	@Autowired MailDao mailDao;

	public void execute() throws Exception {
		List<Mail> mails = readMail.execute();
		
		for (Mail mail : mails) {
			mail.setContent(extractContext(mail));
			mailDao.insert(mail);
		}
		
		workHistoryDao.insert(new WorkHistory(WORK_NAME, new Date()));
	}

	private String extractContext(Mail mail) {
		return mailParserFactory.get(mail.getProviderCode()).parse(mail.getContent());
	}
}
